package com.banmao.CarlCamp.Day01;

/**
 *  https://leetcode.cn/problems/binary-search/
 */
public class LC704 {

    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }

        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = (left + (right - left) / 2);
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        LC704 lc704 = new LC704();
        System.out.println(lc704.search(nums, 2));
    }

}
